/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.proyectos.bean;

import com.proyectos.ejb.CarEjbLocal;
import com.proyectos.model.Carreras;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import org.primefaces.context.RequestContext;


@ManagedBean
/**
 *
 * @author Jose
 */
public class CtrCarreras implements Serializable {

    @EJB
   private CarEjbLocal ejb;
   private String name;
   private List<Carreras> lista;
   private Carreras activeReg;
   private boolean lnuevo;
 

    public boolean isLnuevo() {
        return lnuevo;
    }

    public void setLnuevo(boolean lnuevo) {
        this.lnuevo = lnuevo;
    }



    public Carreras getActiveReg() {
        return activeReg;
    }

    public void setActiveReg(Carreras activeReg) {
        this.activeReg = activeReg;
    }

    public void edit(BigDecimal id) {
        try {
         
            this.lnuevo = false;
            this.activeReg = this.ejb.get(id);
           
            Map<String, Object> options = new HashMap<String, Object>();
            options.put("modal", true);
            options.put("draggable", true);
            options.put("resizable", false);
            options.put("contentHeight", 250);
            options.put("contentWidth",700);
            RequestContext.getCurrentInstance().openDialog("/mantCarreras.xhtml", options, null);
        } catch (Exception ex) {
            Logger.getLogger(CtrCarreras.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void nuevo() {
        try {
            this.lnuevo = true;
            this.activeReg = new Carreras();
           
            Map<String, Object> options = new HashMap<String, Object>();
            options.put("modal", true);
            options.put("draggable", true);
            options.put("resizable", false);
           options.put("contentHeight", 250);
            options.put("contentWidth",700);
            RequestContext.getCurrentInstance().openDialog("/mantCarreras.xhtml", options, null);
        } catch (Exception ex) {
            Logger.getLogger(CtrCarreras.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

   
    public void save(ActionEvent event) {
        try {
            if (this.lnuevo) {
                this.ejb.add(activeReg);
            } else {
                this.ejb.save(activeReg);
            }
        } catch (Exception ex) {
            Logger.getLogger(CtrCarreras.class.getName()).log(Level.SEVERE, null, ex);
        }
   //    RequestContext.getCurrentInstance().update(this.tabla.getClientId());

       
        this.buscar();
        RequestContext.getCurrentInstance().closeDialog(null);

    }

   
    public List<Carreras> getLista() {
        return lista;
    }

    public void setLista(List<Carreras> lista) {
        this.lista = lista;
    }

    public void addMessage(String summary) {
        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, summary, null);
        FacesContext.getCurrentInstance().addMessage(null, message);
    }

    public void buscar() {

        TreeMap vparametros = new TreeMap();
        if (name != null && !name.isEmpty()) {
            vparametros.put("cnombre", "%" + name.toUpperCase() + "%");
        } else {
            vparametros.put("cnombre", "%");
        }
        lista = this.ejb.query("Carreras.findByName", vparametros);

    }

    public void buscar_todo() {

        buscar();

    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public CtrCarreras() {
    }

    public List<Carreras> getVlista() {
        return this.ejb.query("Carreras.findAll", new TreeMap());

    }

    public void delete(ActionEvent event) {
        try {
            this.ejb.delete(activeReg.getNcodigo());
        } catch (Exception ex) {
            Logger.getLogger(CtrCarreras.class.getName()).log(Level.SEVERE, null, ex);
        }
        RequestContext.getCurrentInstance().closeDialog(null);
        this.buscar();
    }
}
